﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers
{
    public class MenusController : MenusControllerBase
    {
        public MenusController()
            : base()
        {
        }
        public static List<MenusEntity> GetMenusByParentModule(int parentId, int moduleId)
        {
            LoadAllMenus(false);
            return ListMenusStatic.FindAll(delegate(MenusEntity o) { return o.ParentId == parentId && o.ModuleId == moduleId; });
        }
        public static MenusEntity GetMenusById(int menuId)
        {
            LoadAllMenus(false);
            return ListMenusStatic.Find(delegate(MenusEntity o) { return o.MenuId == menuId; });
        }
        private DataTable _Table;
        private MenusEntity oMenus;
        public MenusEntity MenusObject
        {
            get
            {
                return oMenus;
            }
            set
            {
                oMenus = value;
            }
        }
        public DataTable Table
        {
            get
            {
                if (_Table == null && AllowCaching)
                {
                    _Table = Cache.Get<DataTable>(CACHE_KEY_DS);
                }
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void LoadAllDSMenus(bool clearCache)
        {
            bool bForce = clearCache;
            if (bForce || _Table == null)
            {
                DataSet ds = Service.SelectAllDS();
                _Table = ds.Tables[0];
                AddIndexToTable(_Table);
                if (AllowCaching)
                    AddCache(CACHE_KEY_DS, _Table);
            }
        }
        public void AddIndexToTable()
        {
            AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            ResetTableIndex(_Table);
        }
        public void LoadObject(Int32 MenuId)
        {
            MenusEntity oLoad = FindMenus(MenuId);
            if (oLoad == null)
            {
                LoadAllMenus(true);
                oMenus = FindMenus(MenuId);
            }
            else
            {
                oMenus = oLoad;
            }
        }
        public bool AddNew()
        {
            if (oMenus == null) return false;
            bool result = Service.Save(oMenus);
            if (result)
            {
                AddObjectToCache(oMenus);
            }
            return result;
        }
        public bool Update()
        {
            if (oMenus == null) return false;
            return Service.Update(oMenus);
        }
        public bool Delete()
        {
            if (oMenus == null) return false;
            bool retvalue = Service.Delete(oMenus);
            if (retvalue)
            {
                RemoveObjectFromCache(oMenus);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 MenuId)
        {
            bool retvalue = Service.DeleteByID(MenuId);
            if (retvalue)
            {
                MenusEntity oDeleted = FindMenus(MenuId);
                RemoveObjectFromCache(oDeleted);
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oMenus = new MenusEntity();
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.MenusObject, row);
        }
        public MenusEntity LoadMenus(Int32 MenuId)
        {
            MenusEntity o = Service.SelectOne(MenuId);
            return o;
        }
        #region Cached
        private static List<MenusEntity> _ListMenus;
        private static bool _AllowCaching = true;
        public static bool AllowCaching
        {
            get { return _AllowCaching; }
            set { AllowCaching = value; }
        }
        public static void ClearCache()
        {
            if (!_AllowCaching) return;
            Cache.Remove(CACHE_KEY_DS);
            Cache.Remove(CACHE_KEY_LS);
        }
        private const string CACHE_KEY_DS = "MenusDS";
        private const string CACHE_KEY_LS = "MenusLS";
        private void AddObjectToCache(MenusEntity newObject)
        {
            if (!AllowCaching) return;
            GetCacheList().Add(newObject);
        }
        private void RemoveObjectFromCache(MenusEntity oRemove)
        {
            if (!AllowCaching) return;
            if (oRemove != null)
                GetCacheList().Remove(oRemove);
        }
        private static List<MenusEntity> GetCacheList()
        {
            if (Cache == null) return default(List<MenusEntity>);
            List<MenusEntity> ls = Cache.Get<List<MenusEntity>>(CACHE_KEY_LS);
            if (ls == null)
            {
                ls = new List<MenusEntity>();
                AddCache(CACHE_KEY_LS, ls);
            }
            return ls;
        }
        public List<MenusEntity> ListMenus
        {
            get
            {
                return LoadAllMenus(false);
            }
        }
        public static List<MenusEntity> ListMenusStatic
        {
            get
            {
                LoadAllMenus(false);
                return _ListMenus;
            }
        }
        public MenusEntity FindMenus(Int32 MenuId)
        {
            if (AllowCaching)
            {
                _ListMenus = GetCacheList();
            }
            MenusEntity oResult = _ListMenus.Find(delegate(MenusEntity oFind)
            {
                bool result = true;
                result &= oFind.MenuId.Equals(MenuId);
                return result;
            });
            return oResult;
        }
        public static List<MenusEntity> LoadAllMenus(bool clearCache)
        {
            _ListMenus = new List<MenusEntity>();
            if (AllowCaching)
            {
                _ListMenus = GetCacheList();
            }
            if (_ListMenus == null || _ListMenus.Count == 0)
                clearCache = true;
            if (!clearCache) return _ListMenus;
            _ListMenus = new List<MenusEntity>(Service.SelectAll());
            if (AllowCaching)
                AddCache(CACHE_KEY_LS, _ListMenus);
            return _ListMenus;
        }
        #endregion
    }
}
